package com.borrow.jiemihua.utils;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.NonNull;

import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;


/**
 * @ Create By xiaolong lin
 * @ Create Time 2017/9/21
 * @ Description { PermissionUtils }
 */
public class PermissionUtils {
    private static final int REQUEST_CODE_PERMISSION = 100;
    private static final int REQUEST_CODE_SETTING = 300;
    private Activity mActivity;
    private Listener mListener;

    public PermissionUtils(Activity activity, Listener listener) {
        mActivity = activity;
        mListener = listener;
    }

    /**
     * 动态获取权限（授权成功后默认授权监听）
     *
     * @param permission
     */
    public void onPermission(String[] permission) {
        AndPermission.with(mActivity)
                .requestCode(REQUEST_CODE_PERMISSION)
                .permission(permission)
                .callback(permissionListener)
                // rationale作用是：用户拒绝一次权限，再次申请时先征求用户同意，再打开授权对话框，这样避免用户勾选不再提示，导致以后无法申请权限。
                .rationale(rationaleListener)
                .start();
    }

    /**
     * 动态获取权限（授权成功后自定义授权监听）
     *
     * @param permission
     * @param permissionListener
     */
    public void onPermission(String[] permission, PermissionListener permissionListener) {
        AndPermission.with(mActivity)
                .requestCode(REQUEST_CODE_PERMISSION)
                .permission(permission)
                .callback(permissionListener)
                // rationale作用是：用户拒绝一次权限，再次申请时先征求用户同意，再打开授权对话框，这样避免用户勾选不再提示，导致以后无法申请权限。
                .rationale(rationaleListener)
                .start();
    }

    /**
     * 回调监听。
     */
    private PermissionListener permissionListener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
            mListener.onPermissionSucceed(grantPermissions.get(0));
        }

        @Override
        public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
            // 用户拒绝授权后，提示用户到设置中授权
            if (AndPermission.hasAlwaysDeniedPermission(mActivity, deniedPermissions)) {
                AndPermission.defaultSettingDialog(mActivity, REQUEST_CODE_SETTING).show();
            }
        }
    };
    /**
     * Rationale支持，这里自定义对话框。
     */
    private RationaleListener rationaleListener = new RationaleListener() {
        @Override
        public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
            AlertDialog.newBuilder(mActivity)
                    .setTitle("提示")
                    .setMessage("我们需要的一些必要权限被禁止，请授权给我们。")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            rationale.resume();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            rationale.cancel();
                        }
                    }).show();
        }
    };


    public interface Listener {
        void onPermissionSucceed(String permission);
    }
}
